import Vue from "vue";
import VueRouter from "vue-router";
import store from '@/store'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

import Home from '@/pages/Home'
import Login from '@/pages/Login'
import Search from '@/pages/Search'
import Register from '@/pages/Register'
import Detail from '@/pages/Detail'
import AddCartSuccess from '@/pages/AddCartSuccess'
import ShopCart from '@/pages/ShopCart'
import Trade from '@/pages/Trade'
import Pay from '@/pages/Pay'
import PaySuccess from '@/pages/PaySuccess'
import Center from '@/pages/Center'
import myOrder from '@/pages/Center/myOrder'
import groupOrder from '@/pages/Center/groupOrder'


let router = new VueRouter({
    routes: [
        {
            path: '/center',
            component: Center,
            meta: {
                isShowFooter: true
            },
            children: [
                {
                    path: 'myorder',
                    component: myOrder
                },
                {
                    path: 'grouporder',
                    component: groupOrder
                },
                {
                    path: '/center',
                    redirect: '/center/myorder'
                }
            ]
        },
        {
            path: '/paysuccess',
            component: PaySuccess,
            meta: {
                isShowFooter: true
            },
            beforeEnter: (to, from, next) => {
                if (from.path == '/pay') {
                    next()
                } else {
                    next(false)
                }
            }
        },
        {
            path: '/pay',
            component: Pay,
            meta: {
                isShowFooter: true
            },
            beforeEnter: (to, from, next) => {
                if (from.path == '/trade') {
                    next()
                } else {
                    next(false)
                }
            }
        },
        {
            path: '/trade',
            component: Trade,
            meta: {
                isShowFooter: true
            },
            beforeEnter: (to, from, next) => {
                if (from.path == '/shopcart') {
                    next()
                } else {
                    next(false)
                }
            }
        },
        {
            path: '/home',
            component: Home,
            meta: {
                isShowFooter: true
            }
        },
        {
            path: '/shopcart',
            name: 'shopcart',
            component: ShopCart,
            meta: {
                isShowFooter: true
            }
        },
        {
            name: "search",
            path: '/search/:keyword?',
            component: Search,
            meta: {
                isShowFooter: true
            }
        },
        {
            path: '/login',
            component: Login,
            meta: {
                isShowFooter: false
            }
        },
        {
            path: '/register',
            component: Register,
            meta: {
                isShowFooter: false
            }
        },
        {
            path: '/detail/:skuId',
            component: Detail,
            meta: {
                isShowFooter: true
            }
        },
        {
            path: '*',
            redirect: '/home'
        },
        {
            path: '/addcart',
            name: 'addcart',
            component: AddCartSuccess,
            meta: {
                isShowFooter: true
            }
        },
    ],
    scrollBehavior(to, from, savedPosition) {
        return { y: 0 }
    },
})

router.beforeEach(async (to, from, next) => {
    next()
    let token = store.state.user.token
    let name = store.state.user.userInfo.name
    if (token) {
        if (to.path == '/login') {
            next('/home')
        } else {
            if (name) {
                next()
            }
            else {
                try {
                    await store.dispatch("getLoginInfo");
                    next()
                } catch (error) {
                    await store.dispatch("getUserLogout");
                    next('/login')
                }
            }
        }
    }
    else {
        let toPath = to.path
        if (toPath.indexOf('/tarde') != -1 || toPath.indexOf('/pay') != -1 || toPath.indexOf('/center') != -1) {
            next('/login?redirect=' + toPath)
        }
        else {
            next()
        }
    }
})

export default router